package com.example.demo.arithmetic;

import java.util.HashSet;

/**
 * Description: 给定一个字符串数组，查找最长公共前缀，没有返回""<br/>
 *
 * @author liulonglong
 * @taskId <br/>
 * @Date 2020/3/11 15:11
 * @Version 1.0
 */
public class LongestCommonPrefix {

    public static void main(String[] args) {
        int huiWen = getHuiWen("abccccdd");
        System.out.println(huiWen);
    }

    public static String getCommonPrefix(String[] arrys) {
        if (arrys.length == 0 || arrys == null) {
            return "";
        }
        int length = arrys.length;
        for (int i = 0; i < length; i++) {

        }
        return "";
    }

    // abccccdd
    public static int getHuiWen(String s) {
        char[] chars = s.toCharArray();
        HashSet<Character> set = new HashSet<>();
        int sum = 0;
        for (int i = 0; i < chars.length; i++) {
            if (!set.contains(chars[i])) {
                set.add(chars[i]);
            } else {
                sum++;
                set.remove(chars[i]);
            }

        }
        return set.isEmpty() ? sum * 2 : sum * 2 + 1;
        /*if (s.length() == 0) {
            return 0;
        }
        // ⽤于存放字符
        HashSet<Character> hashset = new HashSet<>();
        char[] chars = s.toCharArray();
        int count = 0;
        for (int i = 0; i < chars.length; i++) {
            if (!hashset.contains(chars[i])) {// 如果hashset没有该字符就保存进去
                hashset.add(chars[i]);
            } else {// 如果有,就让count++（说明找到了⼀个成对的字符），然后把该字符移除
                hashset.remove(chars[i]);
                count++;
            }
        }
        return hashset.isEmpty() ? count * 2 : count * 2 + 1;*/
    }

}

